/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package MainPackage;


import MainPackage.UserMahlzeit.MahlzeitType;
import MainPackage.Views.*;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.beans.PropertyChangeEvent;
import java.security.NoSuchAlgorithmException;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import javax.swing.table.DefaultTableModel;
import javax.xml.bind.JAXBException;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;

/**
 *
 * @author n.kaminski
 */
public class MainWindow extends javax.swing.JFrame {

    /**
     * Enhält alle User
     */
    private Userlist userlist;
    /**
     * Enhält alle Nahrungsmittel
     */
    private NahrungsmittelList nahrungsmittelList;
    /**
     * Der Eingeloggte User
     */
    private User user; 
    /**
     * Alle verfügbaren Sportarten
     */
    private Sportarten sport;
    
    /**
     * Erstellt neues form MainWindow
     * 
     * Es werden auch alle User und Nahrungsmittel geladen.
     */
    public MainWindow(){
        //Lade die wichtigen daten        
        try {
            //Users
            this.userlist = Userlist.loadUserlist();
            //Nahrungsmittel
            this.nahrungsmittelList = NahrungsmittelList.loadNahrungsmittelList();
            //sportarten
            this.sport = new Sportarten();
        } catch (Exception ex) {
            System.out.println("Fehler");
            System.out.println(ex);
            System.exit(1);
            //Logger.getLogger(MainWindow.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.println("Debug: "+Options.DEBUG);
        if(!Options.DEBUG){
            user = Login.getLoggedinUser(userlist);
            if(user == null){
                System.exit(1);
            }
        }else{
            user = userlist.getUser("DebugUser");
            if(user == null){
                user = new User("DebugUser", "egal",Calendar.getInstance().getTime(),175, User.Geschlecht.maennlich, User.Arbeit.vornehmlichSitzen );
                userlist.addUser(user);
            }
            System.out.println(user);
        }
        
        
        
        initComponents();
        if(!Options.DEBUG){
            this.TestingButton.setVisible(false);
            this.AuswertungButton.setVisible(false);
        }
        this.aktUser.setText("Eingeloggt als: "+user.getName());
        this.userlist.export();
        this.nahrungsmittelList.export(); 
        
        this.AnzeigeDatum.setDate(new Date());
        AnzeigeDatum.addPropertyChangeListener(new java.beans.PropertyChangeListener() {
            @Override
            public void propertyChange(java.beans.PropertyChangeEvent evt) {
                ChangeDateAndRedraw(evt);
            }
        });
        
        this.writeGegessenenListeUndZeichneGraph();
        
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        NamePanel = new javax.swing.JPanel();
        aktUser = new javax.swing.JLabel();
        AnzeigeDatum = new com.toedter.calendar.JDateChooser();
        jLabel4 = new javax.swing.JLabel();
        InhaltsstoffePanel = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        NeueEingabeButton = new javax.swing.JButton();
        TaetigkeitenButton = new javax.swing.JButton();
        GewichtButton = new javax.swing.JButton();
        SollwerteButton = new javax.swing.JButton();
        LebensmittelButton = new javax.swing.JButton();
        AuswertungButton = new javax.swing.JButton();
        LogoutButton = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        TaetigkeitenTabelle = new javax.swing.JTable();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        GegessenenEditorPane = new javax.swing.JEditorPane();
        TestingButton = new javax.swing.JButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        HilfeJMenu = new javax.swing.JMenu();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBackground(new java.awt.Color(240, 240, 240));
        jPanel1.setAlignmentX(0.0F);
        jPanel1.setAlignmentY(0.0F);
        jPanel1.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));

        NamePanel.setBackground(new java.awt.Color(180, 180, 180));
        NamePanel.setPreferredSize(new java.awt.Dimension(0, 25));

        aktUser.setText("jLabel4");

        jLabel4.setText("Datum:");

        javax.swing.GroupLayout NamePanelLayout = new javax.swing.GroupLayout(NamePanel);
        NamePanel.setLayout(NamePanelLayout);
        NamePanelLayout.setHorizontalGroup(
            NamePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(NamePanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(aktUser, javax.swing.GroupLayout.PREFERRED_SIZE, 334, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(AnzeigeDatum, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        NamePanelLayout.setVerticalGroup(
            NamePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(NamePanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(NamePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(AnzeigeDatum, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(NamePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(aktUser)
                        .addComponent(jLabel4)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        InhaltsstoffePanel.setBackground(new java.awt.Color(255, 255, 255));

        jPanel2.setBackground(NamePanel.getBackground());
        jPanel2.setPreferredSize(new java.awt.Dimension(0, 15));

        jLabel1.setText("Inhaltsstoffe");

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jLabel1)
                .addGap(0, 115, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(jLabel1))
        );

        javax.swing.GroupLayout InhaltsstoffePanelLayout = new javax.swing.GroupLayout(InhaltsstoffePanel);
        InhaltsstoffePanel.setLayout(InhaltsstoffePanelLayout);
        InhaltsstoffePanelLayout.setHorizontalGroup(
            InhaltsstoffePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, 204, Short.MAX_VALUE)
        );
        InhaltsstoffePanelLayout.setVerticalGroup(
            InhaltsstoffePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(InhaltsstoffePanelLayout.createSequentialGroup()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );

        NeueEingabeButton.setBackground(jPanel1.getBackground());
        NeueEingabeButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GFX/NeueEingabe.png"))); // NOI18N
        NeueEingabeButton.setText("Neue Eingabe");
        NeueEingabeButton.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        NeueEingabeButton.setBorderPainted(false);
        NeueEingabeButton.setDoubleBuffered(true);
        NeueEingabeButton.setFocusPainted(false);
        NeueEingabeButton.setFocusable(false);
        NeueEingabeButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        NeueEingabeButton.setMargin(new java.awt.Insets(2, 2, 2, 2));
        NeueEingabeButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        NeueEingabeButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                NeueEingabeButtonActionPerformed(evt);
            }
        });

        TaetigkeitenButton.setBackground(jPanel1.getBackground());
        TaetigkeitenButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GFX/Taetigkeit.png"))); // NOI18N
        TaetigkeitenButton.setText("Tätigkeiten");
        TaetigkeitenButton.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        TaetigkeitenButton.setBorderPainted(false);
        TaetigkeitenButton.setDoubleBuffered(true);
        TaetigkeitenButton.setFocusPainted(false);
        TaetigkeitenButton.setFocusable(false);
        TaetigkeitenButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        TaetigkeitenButton.setMargin(new java.awt.Insets(2, 2, 2, 2));
        TaetigkeitenButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        TaetigkeitenButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                TaetigkeitenButtonActionPerformed(evt);
            }
        });

        GewichtButton.setBackground(jPanel1.getBackground());
        GewichtButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GFX/Gewicht.png"))); // NOI18N
        GewichtButton.setText("Gewicht");
        GewichtButton.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        GewichtButton.setBorderPainted(false);
        GewichtButton.setDoubleBuffered(true);
        GewichtButton.setFocusPainted(false);
        GewichtButton.setFocusable(false);
        GewichtButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        GewichtButton.setMargin(new java.awt.Insets(2, 2, 2, 2));
        GewichtButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        GewichtButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                GewichtButtonActionPerformed(evt);
            }
        });

        SollwerteButton.setBackground(jPanel1.getBackground());
        SollwerteButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GFX/Sollwerte.png"))); // NOI18N
        SollwerteButton.setText("Sollwerte");
        SollwerteButton.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        SollwerteButton.setBorderPainted(false);
        SollwerteButton.setDoubleBuffered(true);
        SollwerteButton.setFocusPainted(false);
        SollwerteButton.setFocusable(false);
        SollwerteButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        SollwerteButton.setMargin(new java.awt.Insets(2, 2, 2, 2));
        SollwerteButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        SollwerteButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                SollwerteButtonActionPerformed(evt);
            }
        });

        LebensmittelButton.setBackground(jPanel1.getBackground());
        LebensmittelButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GFX/Lebensmittel.png"))); // NOI18N
        LebensmittelButton.setText("Lebensmittel");
        LebensmittelButton.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        LebensmittelButton.setBorderPainted(false);
        LebensmittelButton.setDoubleBuffered(true);
        LebensmittelButton.setFocusPainted(false);
        LebensmittelButton.setFocusable(false);
        LebensmittelButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        LebensmittelButton.setMargin(new java.awt.Insets(2, 2, 2, 2));
        LebensmittelButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        LebensmittelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                LebensmittelButtonActionPerformed(evt);
            }
        });

        AuswertungButton.setBackground(jPanel1.getBackground());
        AuswertungButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GFX/Auswertung.png"))); // NOI18N
        AuswertungButton.setText("Auswertung");
        AuswertungButton.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        AuswertungButton.setBorderPainted(false);
        AuswertungButton.setDoubleBuffered(true);
        AuswertungButton.setFocusPainted(false);
        AuswertungButton.setFocusable(false);
        AuswertungButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        AuswertungButton.setMargin(new java.awt.Insets(2, 2, 2, 2));
        AuswertungButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);

        LogoutButton.setBackground(jPanel1.getBackground());
        LogoutButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GFX/Logout.png"))); // NOI18N
        LogoutButton.setText("Logout");
        LogoutButton.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        LogoutButton.setBorderPainted(false);
        LogoutButton.setDoubleBuffered(true);
        LogoutButton.setFocusPainted(false);
        LogoutButton.setFocusable(false);
        LogoutButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        LogoutButton.setMargin(new java.awt.Insets(2, 2, 2, 2));
        LogoutButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        LogoutButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                LogoutButtonActionPerformed(evt);
            }
        });

        jPanel3.setBackground(NamePanel.getBackground());
        jPanel3.setAlignmentX(0.1F);
        jPanel3.setAlignmentY(0.1F);

        TaetigkeitenTabelle.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Art", "Dauer"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(TaetigkeitenTabelle);

        jLabel2.setText("Tätigkeiten");

        jLabel3.setText("Gegessene Liste");

        GegessenenEditorPane.setEditable(false);
        jScrollPane2.setViewportView(GegessenenEditorPane);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3))
                .addGap(0, 0, Short.MAX_VALUE))
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addComponent(jLabel2)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 109, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 391, Short.MAX_VALUE)
                .addContainerGap())
        );

        TestingButton.setBackground(jPanel1.getBackground());
        TestingButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/GFX/TEST.png"))); // NOI18N
        TestingButton.setText("TestSomething");
        TestingButton.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        TestingButton.setBorderPainted(false);
        TestingButton.setDoubleBuffered(true);
        TestingButton.setFocusPainted(false);
        TestingButton.setFocusable(false);
        TestingButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        TestingButton.setMargin(new java.awt.Insets(2, 2, 2, 2));
        TestingButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        TestingButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                TestingButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(NamePanel, javax.swing.GroupLayout.DEFAULT_SIZE, 786, Short.MAX_VALUE)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(NeueEingabeButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(TaetigkeitenButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(GewichtButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(SollwerteButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(LebensmittelButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(AuswertungButton)
                        .addGap(18, 18, 18)
                        .addComponent(TestingButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(LogoutButton))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(InhaltsstoffePanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addComponent(NamePanel, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(NeueEingabeButton, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(TaetigkeitenButton, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(GewichtButton, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(SollwerteButton, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(LebensmittelButton, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(AuswertungButton, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(LogoutButton, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(TestingButton, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(InhaltsstoffePanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );

        getContentPane().add(jPanel1, java.awt.BorderLayout.PAGE_END);

        HilfeJMenu.setText("Hilfe");
        HilfeJMenu.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                HilfeJMenuMousePressed(evt);
            }
        });
        HilfeJMenu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                HilfeJMenuActionPerformed(evt);
            }
        });
        jMenuBar1.add(HilfeJMenu);

        setJMenuBar(jMenuBar1);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    
    /**
     * Klick auf den Button zum Testen von bestimmten funktionen.
     * Der Button kann nur betätigt werden, wenn der @see Options.DEBUG == true ist.
     * @param evt 
     */
    private void TestingButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_TestingButtonActionPerformed
        GewichtPlottingDialog gpd = new GewichtPlottingDialog(this, true);
        gpd.setVisible(true);
    }//GEN-LAST:event_TestingButtonActionPerformed

    /**
     * Klick auf den Lebensmittel Button
     * Zeigt alle Lebensmittel in neuem Fenster, um Neue zu erstellen oder Vorhandene zu bearbeiten.
     * @param evt 
     */
    private void LebensmittelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_LebensmittelButtonActionPerformed
        LebensmittelView lv = new LebensmittelView(this, true, this.nahrungsmittelList);
        lv.setVisible(true);
        this.nahrungsmittelList.export();
    }//GEN-LAST:event_LebensmittelButtonActionPerformed
    
    /**
     * Klick auf Gewicht Button
     * Zeigt die die Übersicht über das Gewicht udn den BMI an.
     * @param evt 
     */
    private void GewichtButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_GewichtButtonActionPerformed
        GewichtView gv = new GewichtView(this.user, this.userlist, this, true);
        gv.setVisible(true);
    }//GEN-LAST:event_GewichtButtonActionPerformed

    /**
     * Klick auf Neue Eingabe
     * Zeigt das Fenster um neue Eingaben / Mahlzeiten zu tätigen.
     * @param evt 
     */
    private void NeueEingabeButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_NeueEingabeButtonActionPerformed
        MahzeitEintragen me = new MahzeitEintragen(this, true, this.nahrungsmittelList, this.user);  
        me.setVisible(true);
        userlist.export();
        writeGegessenenListeUndZeichneGraph();
    }//GEN-LAST:event_NeueEingabeButtonActionPerformed

    /**
     * Klick auf Sollwerte
     * Zeigt ein Fenster mit den Solwerten für den User an.
     * @param evt 
     */
    private void SollwerteButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_SollwerteButtonActionPerformed
        // TODO add your handling code here:
        SolwerteView sw = new SolwerteView(this, true, user);
        sw.setVisible(true);
    }//GEN-LAST:event_SollwerteButtonActionPerformed

    /**
     * Klick auf Tätigkeiten
     * Zigt das Fenster um Sport für bestimmte Tage einzutragen.
     * @param evt 
     */
    private void TaetigkeitenButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_TaetigkeitenButtonActionPerformed
        TaetigkeitenView tv = new TaetigkeitenView(this, true, user, sport);
        tv.setVisible(true);
        this.userlist.export();
        writeGegessenenListeUndZeichneGraph();
    }//GEN-LAST:event_TaetigkeitenButtonActionPerformed

    
    private void ChangeDateAndRedraw (PropertyChangeEvent evt)  { 
        this.writeGegessenenListeUndZeichneGraph();
    }
    
    /**
     * Klick auf Logout
     * Loggt den aktuellen User aus und zeigt das Login Fenster an. Wenn dieses geschlossen wird, so wird auch das gesammte Programm beendet.
     * @param evt 
     */
    private void LogoutButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_LogoutButtonActionPerformed
        this.setVisible(false);
        this.user = Login.getLoggedinUser(this.userlist);        
        if(user == null){
            this.setVisible(false);
            System.exit(0);
        }
        this.setVisible(true);
        this.writeGegessenenListeUndZeichneGraph();
        this.aktUser.setText("Eingeloggt als: "+user.getName());
    }//GEN-LAST:event_LogoutButtonActionPerformed

    private void HilfeJMenuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_HilfeJMenuActionPerformed
        System.out.println("Hilfe");
        HilfeView h=new HilfeView(this, true);
        h.setVisible(true);
        
    }//GEN-LAST:event_HilfeJMenuActionPerformed

    private void HilfeJMenuMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_HilfeJMenuMousePressed
        // TODO add your handling code here:
        HilfeView hv = new HilfeView(this, false);
        hv.setVisible(true);
    }//GEN-LAST:event_HilfeJMenuMousePressed

    /**
     * Main Methode
     * Ruft @see Options.initializeAllFilesAndFolders() auf und erstellt dann das MainWindow und zeigt diese an.
     * @param args the command line arguments
     */
    public static void main(String args[]) throws JAXBException, NoSuchAlgorithmException {
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        /*try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MainWindow.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }*/
        //</editor-fold>

        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                Options.initializeAllFilesAndFolders();
                MainWindow mw = new MainWindow();                
                
                mw.setVisible(true);
            }

        });

    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private com.toedter.calendar.JDateChooser AnzeigeDatum;
    private javax.swing.JButton AuswertungButton;
    private javax.swing.JEditorPane GegessenenEditorPane;
    private javax.swing.JButton GewichtButton;
    private javax.swing.JMenu HilfeJMenu;
    private javax.swing.JPanel InhaltsstoffePanel;
    private javax.swing.JButton LebensmittelButton;
    private javax.swing.JButton LogoutButton;
    private javax.swing.JPanel NamePanel;
    private javax.swing.JButton NeueEingabeButton;
    private javax.swing.JButton SollwerteButton;
    private javax.swing.JButton TaetigkeitenButton;
    private javax.swing.JTable TaetigkeitenTabelle;
    private javax.swing.JButton TestingButton;
    private javax.swing.JLabel aktUser;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    // End of variables declaration//GEN-END:variables
    
    
    /**
     * Zeigt die Userdaten auf dem Form an
     * Zeichnet das Balkendiagramm auf der linken Seite, lädt die Sportaktivitäten(rechts oben), erstellt die "Gegessenen liste" (rechts unten)
     * Alle Daten sind vom aktuellem Tag.
     */
    private void writeGegessenenListeUndZeichneGraph(){
        //Date heute = new Date();
        Date heute = this.AnzeigeDatum.getDate();
        String[] ColNames = {"Art", "Dauer"};
        DefaultTableModel dtm = new DefaultTableModel(ColNames, 0);
        List<UserSport> us = user.getSport();
        
        double pluskcalsport = 0;
        
        for(UserSport u : us){
            Date d = u.getDatum();
            if(d.getYear() == heute.getYear() && d.getMonth() == heute.getMonth() && d.getDate() == heute.getDate()){
                Object[] o = new Object[2];
                o[0] = u.getName();
                o[1] = u.getDauer()+" Minuten";
                dtm.addRow(o);
                
                pluskcalsport += sport.getKCAL(u.getName(), user)*u.getDauer();
            }
        }
        TaetigkeitenTabelle.setModel(dtm);
        //dtm.setValueAt("Kilo Joule (kj)", 0, 0);
        //dtm.setValueAt(this.getKcalAsKJoule(), 0, 1);
        
        Nahrungsmittel tageswerte = new Nahrungsmittel();
        
        StringBuilder sb = new StringBuilder();
        
        sb.append("<div style=\"font-size:small\" >");
        Date d = heute;
        List<UserMahlzeit> um = user.getMahlzeiten_ByDatum(d.getDate(), d.getMonth(), d.getYear());
        
        sb.append("<b><u>Frühstück:</u></b><br/><ul>");
        for(int i = 0; i < um.size(); ++i){
            if(um.get(i).getMType() == MahlzeitType.Fruehstueck && um.get(i).getNahrungsmittelID() > 0){
                sb.append("<li>");
                sb.append(nahrungsmittelList.getNahrungsmittelbyID(um.get(i).getNahrungsmittelID()).getName());
                sb.append(" - ");
                sb.append(um.get(i).getMenge());
                sb.append(" g </li>");
                tageswerte.AddOtherNahrungsmittel(nahrungsmittelList.getNahrungsmittelbyID(um.get(i).getNahrungsmittelID()), um.get(i).getMenge());
                
            }
        }
        sb.append("</ul><b><u>Vormittags:</u></b><br/><ul>");
        for(int i = 0; i < um.size(); ++i){
            if(um.get(i).getMType() == MahlzeitType.Vormittags && um.get(i).getNahrungsmittelID() > 0){
                sb.append("<li>");
                sb.append(nahrungsmittelList.getNahrungsmittelbyID(um.get(i).getNahrungsmittelID()).getName());
                sb.append(" - ");
                sb.append(um.get(i).getMenge());
                sb.append(" g </li>");
                
                tageswerte.AddOtherNahrungsmittel(nahrungsmittelList.getNahrungsmittelbyID(um.get(i).getNahrungsmittelID()), um.get(i).getMenge());
                
            }
        }
        sb.append("</ul><b><u>Mittagessen:</u></b><br/><ul>");
        for(int i = 0; i < um.size(); ++i){
            if(um.get(i).getMType() == MahlzeitType.Mittagessen && um.get(i).getNahrungsmittelID() > 0){
                sb.append("<li>");
                sb.append(nahrungsmittelList.getNahrungsmittelbyID(um.get(i).getNahrungsmittelID()).getName());
                sb.append(" - ");
                sb.append(um.get(i).getMenge());
                sb.append(" g </li>");
                tageswerte.AddOtherNahrungsmittel(nahrungsmittelList.getNahrungsmittelbyID(um.get(i).getNahrungsmittelID()), um.get(i).getMenge());
            }
        }
        sb.append("</ul><b><u>Nachmittags:</u></b><br/><ul>");
        for(int i = 0; i < um.size(); ++i){
            if(um.get(i).getMType() == MahlzeitType.Nachmittags && um.get(i).getNahrungsmittelID() > 0){
                sb.append("<li>");
                sb.append(nahrungsmittelList.getNahrungsmittelbyID(um.get(i).getNahrungsmittelID()).getName());
                sb.append(" - ");
                sb.append(um.get(i).getMenge());
                sb.append(" g </li>");
                tageswerte.AddOtherNahrungsmittel(nahrungsmittelList.getNahrungsmittelbyID(um.get(i).getNahrungsmittelID()), um.get(i).getMenge());
            }
        }
        sb.append("</ul><b><u>Abendessen:</u></b><br/><ul>");
        for(int i = 0; i < um.size(); ++i){
            if(um.get(i).getMType() == MahlzeitType.Abendessen && um.get(i).getNahrungsmittelID() > 0){
                sb.append("<li>");
                sb.append(nahrungsmittelList.getNahrungsmittelbyID(um.get(i).getNahrungsmittelID()).getName());
                sb.append(" - ");
                sb.append(um.get(i).getMenge());
                sb.append(" g </li>");
                tageswerte.AddOtherNahrungsmittel(nahrungsmittelList.getNahrungsmittelbyID(um.get(i).getNahrungsmittelID()), um.get(i).getMenge());
            }
        }
        sb.append("</ul>");
        sb.append("</div>");
        
        this.GegessenenEditorPane.setContentType("text/html");
        this.GegessenenEditorPane.setText(sb.toString() );
        
        InhaltsstoffePanel.setLayout(new GridLayout(1,1));
        UserReferenzwerte ref = user.getUserReferenzwerte();
        // Create a simple Bar chart
        
        
        
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        dataset.setValue((tageswerte.getKcal() / (ref.getKcal()+pluskcalsport))*100, "A", "kcal");
        dataset.setValue((tageswerte.getBroteinheiten() / ref.getBroteinheiten())*100, "A", "Broteinheiten");
        dataset.setValue( (tageswerte.getKohlenhydrate() / ref.getKohlenhydrate())*100 , "A", "Kohlenhydrate");
        dataset.setValue( (tageswerte.getEiweiss_g() / ref.getEiweiss_g())*100 , "A", "Eiweiß ");
        dataset.setValue( (tageswerte.getCholesterin_mg() / ref.getCholesterin_mg())*100, "A", "Cholesterin");
        dataset.setValue( (tageswerte.getFett_g() / ref.getFett_g())*100 , "A", "Fett");
        dataset.setValue( (tageswerte.getUngesFS_g() / ref.getUngesFS_g())*100, "A", "mehrf. unges. FS");
        dataset.setValue( (tageswerte.getNatrium_mg() / ref.getNatrium_mg())*100, "A", "Natrium");
        dataset.setValue( (tageswerte.getKalium_mg() / ref.getKalium_mg())*100, "A", "Kalium");
        dataset.setValue( (tageswerte.getMagnesium_mg() / ref.getMagnesium_mg())*100, "A", "Magnesium");
        dataset.setValue( (tageswerte.getCalcium_mg() / ref.getCalcium_mg())*100, "A", "Calcium");
        dataset.setValue( (tageswerte.getPhosphor_mg() / ref.getPhosphor_mg())*100, "A", "Phosphor");
        dataset.setValue( (tageswerte.getEisen_mg() / ref.getEisen_mg())*100, "A", "Eisen");
        dataset.setValue( (tageswerte.getZink_mg() / ref.getZink_mg())*100, "A", "Zink");
        dataset.setValue( (tageswerte.getVitamin_A_mueg() / ref.getVitamin_A_mueg())*100, "A", "Vitamin A");
        dataset.setValue( (tageswerte.getVitamin_B1_mg() / ref.getVitamin_B1_mg())*100, "A", "Vitamin B1");
        dataset.setValue( (tageswerte.getVitamin_B2_mg() / ref.getVitamin_B2_mg())*100, "A", "Vitamin B2");
        dataset.setValue( (tageswerte.getVitamin_B6_mg() / ref.getVitamin_B6_mg())*100, "A", "Vitamin B6");
        dataset.setValue( (tageswerte.getVitamin_C_mg() / ref.getVitamin_C_mg())*100, "A", "Vitamin C");
        dataset.setValue( (tageswerte.getVitamin_E_mg() / ref.getVitamin_E_mg())*100, "A", "Vitamin E");
        dataset.setValue( (tageswerte.getFolsaeure_mueg() / ref.getFolsaeure_mueg())*100, "A", "Folsäure");
        dataset.setValue( (tageswerte.getCarotin_mg() / ref.getCarotin_mg())*100, "A", "Carotin");
        
        
        JFreeChart chart = ChartFactory.createBarChart("", "", "% des Heutigen Tagesbedarfs", dataset, PlotOrientation.HORIZONTAL, false, true, false);
        
        ChartPanel chartPanel = new ChartPanel(chart, false, false, false, false, false);
        chartPanel.setPopupMenu(null);
        chartPanel.setPreferredSize(new java.awt.Dimension(450, 270));        
        
        chartPanel.setEnabled(false);
        chartPanel.setDomainZoomable(true);
        
        //InhaltsstoffePanel.getComponent(0);
        
        InhaltsstoffePanel.removeAll();
        InhaltsstoffePanel.add(chartPanel);
        this.setSize(this.getWidth()+1, this.getHeight()+1);
        this.setSize(this.getWidth()-1, this.getHeight()-1);
    }


}
